package com.xy.exam.service.impl;

import com.xy.exam.Exception.ServiceException;
import com.xy.exam.dao.mapper.ExamUserMapper;
import com.xy.exam.pojo.dto.ExamUserAddNewDTO;
import com.xy.exam.pojo.dto.ExamUserUpdateDTO;
import com.xy.exam.pojo.dto.FrontExamAddNewDTO;
import com.xy.exam.pojo.entity.ExamUser;
import com.xy.exam.pojo.vo.ExamUserListItemVO;
import com.xy.exam.pojo.vo.ExamUserStandardVO;
import com.xy.exam.service.IExamService;
import com.xy.exam.service.IExamUserService;
import com.xy.exam.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ExamUserServiceImpl implements IExamUserService {
    @Autowired
    ExamUserMapper examUserMapper;
    @Override
    public void addNew(ExamUserAddNewDTO examAddNewDTO) {
        ExamUser examUser = new ExamUser();
        examUser.setCreateTime(new Date());
        BeanUtils.copyProperties(examAddNewDTO, examUser);
        int insert = examUserMapper.insert(examUser);
        if (insert != 1) {
            String message = "添加考生失败,该考生标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        ExamUserStandardVO standardById = examUserMapper.getStandardById(id);
        if (standardById == null) {
            String message = "删除考生失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int deleteById = examUserMapper.deleteById(id);
        if (deleteById !=1) {
            String message = "删除考生失败，服务器忙，请稍后再尝试!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateExamUserById(Long id, ExamUserUpdateDTO examUpdateDTO) {
        ExamUserStandardVO standardById = examUserMapper.getStandardById(id);
        if (standardById == null) {
            String message = "修改考生详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        ExamUser examUser = new ExamUser();
        examUser.setId(id);
        BeanUtils.copyProperties(examUpdateDTO,examUser);
        int rows = examUserMapper.update(examUser);
        if (rows != 1) {
            String message = "修改考生详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ExamUserStandardVO getStandardById(Long id) {
        ExamUserStandardVO standardById = examUserMapper.getStandardById(id);
        if (standardById == null) {
            String message = "获取用户详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardById;
    }

    @Override
    public List<ExamUserListItemVO> list() {
        List<ExamUserListItemVO> list = examUserMapper.list();
        return list;
    }

    @Override
    public void frontAddNew(FrontExamAddNewDTO frontExamAddNewDTO) {
        String applyName = frontExamAddNewDTO.getApplyName();
        Long examId = frontExamAddNewDTO.getId();
        int countByApplyName = examUserMapper.countByApplyNameAndExamId(applyName,examId);
        if (countByApplyName != 0 ) {
            String message = "该考生已经存在，请勿重复报名！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        ExamUser examUser = new ExamUser();
        examUser.setExamId(frontExamAddNewDTO.getId());
        examUser.setApplyName(frontExamAddNewDTO.getApplyName());
        examUser.setIdNumber(frontExamAddNewDTO.getIdNumber());
        examUser.setCreateTime(new Date());
        //获取当前登录的用户名，目前暂未实现
        //examUser.setUserId();
        //报名状态
        examUser.setStatus(1);
        //添加随机的用户准考证号
        examUser.setExamNumber(UUID.randomUUID().toString());
        int insert = examUserMapper.insert(examUser);
        if (insert == 0) {
            String message = "添加用户失败，服务正忙！";
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, message);
        }
    }
}
